package com.vichat.common.util;

import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by loveit on 2017/4/17.
 */
public class StringUtil {

    /**
     * 16进制字符串转换为字符串
     *
     * @param s
     * @return
     */
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "utf-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    public static long[] stringToLong(String stringArray[]) {
        if (stringArray == null || stringArray.length < 1) {
            return null;
        }
        long longArray[] = new long[stringArray.length];
        for (int i = 0; i < longArray.length; i++) {
            try {
                longArray[i] = Long.valueOf(stringArray[i]);
            } catch (NumberFormatException e) {
                longArray[i] = 0;
                continue;
            }
        }
        return longArray;
    }

    public static String generateMid() {
        //通过当前时间毫秒值和随机函数 获取一个随机数
        String token = System.currentTimeMillis() + new Random().nextInt() + "";
        //由于上述获取的随机数长度不定  所以要采用数据摘要类获取固定长度的随机数  MessageDigest
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] md5 = md.digest(token.getBytes());
            //又由于md5数组如果转换为字符串基本上会是乱码  所以要用Base64Encoder 来进行转换  成字符串
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(md5);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException();
        }
    }

    public static String getOnlyId() {
        return UUID.randomUUID().toString();
    }

    /**
     * 删除换行符，空格符
     *
     * @return
     */
    public static String removeUnqChars(String string) {
        if (string == null) {
            return null;
        }
        return string.trim().replace("\n", "").replace("\r", "");
    }

    public static boolean isSpecialChar(String str) {
        String regEx = "[ @`~!#$%^&*()+=|{}':;',\\[\\].<>/?~！#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static boolean stringToBoolean(String str) {
        if("Y".equalsIgnoreCase(str) || "TRUE".equalsIgnoreCase(str)) {
            return true;
        }else {
            return false;
        }
    }

    public static void main(String[] args) {
        //String msg = "3C696D67207372633D272E2E2F737973636F6E6669672F656D6F74696F6E2F30342E676966273E3C696D67207372633D272E2E2F737973636F6E6669672F656D6F74696F6E2F30342E676966273E3C696D67207372633D272E2E2F737973636F6E6669672F656D6F74696F6E2F30342E676966273E3C696D67207372633D272E2E2F737973636F6E6669672F656D6F74696F6E2F30342E676966273E;";
        //String str = StringUtil.hexStringToString(msg);
        System.out.println(generateMid());
        System.out.println(getOnlyId());

    }

    public static String getI18nMessage(String msg , Object[] objects){
        for (int i=0 ; i<objects.length ; i++){
            msg = msg.replace("{"+i+"}", (String)objects[i]);
        }
        return msg;
    }

}
